package com.cloudpivot.project.crmprojectnew.controller;

import com.cloudpivot.project.crmprojectnew.pojo.ResultJson;
import com.cloudpivot.project.crmprojectnew.pojo.task.Task;
import com.cloudpivot.project.crmprojectnew.service.impl.TaskSerivceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

/**
 * crm任务 中台接口
 */
@Controller
@Slf4j
@RequestMapping("/crmTask")
public class TaskController {

    @Autowired
    private TaskSerivceImpl taskSerivce;

    /**
     * 测试接口用
     */
    @GetMapping("/testTask")
    @ResponseBody
    public ResultJson testTask() {
        return new ResultJson(1, "测试接口调用成功");
    }

    /**
     * 新增crm任务
     */
    @PostMapping("/addTask")
    @ResponseBody
    public ResultJson addTask(@Validated @RequestBody Task task) {
        log.info("接受request对象:{}", task.toString());
        Task serviceTask = this.taskSerivce.addTask(task);
        if (serviceTask.getNew_num() != null) {
            return new ResultJson<Task>(1, "创建任务记录成功", "", serviceTask);
        }
        return new ResultJson(0, "创建任务记录失败");

    }

    /**
     * 删除crm任务
     *
     * @param
     * @return
     */
    @PostMapping("/delTask")
    @ResponseBody
    public ResultJson delTask(@RequestBody Task task) {
        log.info("接受request对象:{}", task.toString());
        String result = this.taskSerivce.delTask(task);
        if (result.equals("1")) {
            return new ResultJson(1, "删除crm任务成功");
        } else {
            return new ResultJson(0, "删除crm任务失败");
        }
    }

    /**
     * 查询crm任务
     *
     * @param user 用户guid
     * @return
     */
    @GetMapping("/listTask")
    @ResponseBody
    public ResultJson<Task> listTask(@RequestParam(value = "user") String user) {
        log.info("接受request str:" + user);
        return null;
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultJson exceptionHandler(MethodArgumentNotValidException e) {
        ResultJson resultJson = new ResultJson(-1, "error");
        resultJson.setMsg(e.getBindingResult().getFieldError().getDefaultMessage());
        return resultJson;
    }
}
